<?php

namespace common\models\c2\entity;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\helpers\ArrayHelper;
use yii\validators\RequiredValidator;

/**
 * This is the model class for table "{{%warehouse_note}}".
 *
 * @property string $id
 * @property integer $type
 * @property string $ref_note_id
 * @property string $label
 * @property string $warehouse_id
 * @property string $receiver_name
 * @property string $updated_by
 * @property string $created_by
 * @property string $memo
 * @property integer $state
 * @property integer $status
 * @property integer $position
 * @property string $created_at
 * @property string $updated_at
 */
class WarehouseNote extends \kzo\base\models\ActiveRecord
{

    public $items;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%warehouse_note}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'ref_note_id', 'warehouse_id', 'updated_by', 'created_by', 'state', 'status', 'position'], 'integer'],
            [['memo'], 'string'],
            [['items'], 'safe'],
            [['items'], 'validateItems'],
            [['created_at', 'updated_at'], 'safe'],
            [['label', 'receiver_name'], 'string', 'max' => 255],
        ];
    }

    public function validateItems($attribute)
    {
        // $requiredValidator = new RequiredValidator();
        // foreach ($this->$attribute as $index => $row) {
        //     $error = null;
        //     $requiredValidator->validate($row['package_id'], $error);
        //     if (!empty($error)) {
        //         $key = $attribute . '[' . $index . '][package_id]';
        //         $this->addError($key, Yii::t('app.c2', '{attribute} can not be empty!', ['attribute' => Yii::t('app.c2', 'Product')]));
        //     }
        // }
    }

    public function loadItems()
    {
        $this->items = $this->getWarehouseNoteItems()->all();
    }

    public function getWarehouseNoteItems()
    {
        return $this->hasMany(WarehouseNoteItem::className(), ['warehouse_note_id' => 'id']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'type' => Yii::t('app.c2', 'Type'),
            'ref_note_id' => Yii::t('app.c2', 'Ref Note ID'),
            'label' => Yii::t('app.c2', 'Label'),
            'warehouse_id' => Yii::t('app.c2', 'Warehouse ID'),
            'receiver_name' => Yii::t('app.c2', 'Receiver Name'),
            'updated_by' => Yii::t('app.c2', 'Updated By'),
            'created_by' => Yii::t('app.c2', 'Created By'),
            'memo' => Yii::t('app.c2', 'Memo'),
            'state' => Yii::t('app.c2', 'State'),
            'status' => Yii::t('app.c2', 'Status'),
            'position' => Yii::t('app.c2', 'Position'),
            'created_at' => Yii::t('app.c2', 'Created At'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            BlameableBehavior::className()
        ]); // TODO: Change the autogenerated stub
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\WarehouseNoteQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\WarehouseNoteQuery(get_called_class());
    }
    
    /**
    * setup default values
    **/
    public function loadDefaultValues($skipIfSet = true) {
        parent::loadDefaultValues($skipIfSet);
    }

}
